package com.lovo.invoicing.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.bean.RoleBean;
import com.lovo.invoicing.bean.LimitBean;
import com.lovo.invoicing.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    IRoleService roleService;
    @RequestMapping("/findRolesByPage")
    public IPage<RoleBean> findRolesByPage(@RequestBody PageBean pageBean){
        return roleService.queryRolesByPage(pageBean);
    }

    @RequestMapping("/addRole")
    public String addRole(String roleName, int[] limitIds){
        if (limitIds.length==0){
            return "error";
        }
        roleService.addRole(roleName,limitIds);
        return "success";
    }

    @RequestMapping("/findRoleList")
    public List<RoleBean> findRoleList(){
        return roleService.queryRoles();
    }

    @RequestMapping("/findRoleIdsByUserId")
    public List<Integer> findRoleIdsByUserId(int userId){
           return roleService.queryRoleIdsByUserId(userId);
    }

    @RequestMapping("findLimitsByRoleIds")
    public List<String> findLimitsByRoleIds(int[] roleIds){
        if (roleIds.length>0){
            return roleService.queryLimitsByRoleIds(roleIds);
        }
         return null;
    }

    @RequestMapping("/removeRole")
    public String removeRole(int roleId){
        roleService.deleteRoleById(roleId);
        return "success";
    }

    @RequestMapping("/modifyRoleLimit")
    public String modifyRoleLimit(int roleId,int[] limitIds){
        if (limitIds.length==0){
            return "error";
        }
        roleService.updateRoleLimit(roleId,limitIds);
        return "success";
    }
    @RequestMapping("/findLimitBeans")
    public List<LimitBean> findLimitBeans(){
        return roleService.queryLimitBeans();
    }

    @RequestMapping("/findLimitDescByRoleId")
    public List<String> findLimitDescByRoleId(int roleId){
         return roleService.queryLimitDescByRoleId(roleId);
    }

    @RequestMapping("/findLimitIdsByRoleId")
    public List<Integer> findLimitIdsByRoleId(int roleId){
        return roleService.queryLimitIdsByRoleId(roleId);
    }
}
